package com.hospital.hms.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.core.bean.BeanUtil;
import com.hospital.hms.common.CommonResult;
import com.hospital.hms.controller.form.*;
import com.hospital.hms.service.DoctorWorkPlanScheduleService;
import com.hospital.hms.utils.HMSErrorCode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/doctor/work_plan/schedule")
@CrossOrigin(origins = "http://localhost:4000", allowCredentials = "true")
@Slf4j
@Tag(name = "DoctorWorkPlanScheduleController", description = "医生出诊管理接口")
public class DoctorWorkPlanScheduleController {
    @Autowired
    private DoctorWorkPlanScheduleService doctorWorkPlanScheduleService;

    @Operation(summary = "通过诊室ID和日期查询医生出诊信息")
    @PostMapping("/selectDoctorScheduleByDeptSubIdAndDate")
    @SaCheckPermission(value = {"ROOT", "SCHEDULE:SELECT"}, mode = SaMode.OR)
    public CommonResult selectDoctorScheduleByDeptSubIdAndDate(@RequestBody @Valid SelectDoctorScheduleByDeptSubIdAndDateForm form) {
        try {
            // 将表单数据转换为 Map
            Map<String, Object> param = BeanUtil.beanToMap(form);
            // 查询医生出诊排班数据
            ArrayList<HashMap> scheduleData = doctorWorkPlanScheduleService.selectDoctorScheduleByDeptSubIdAndDate(param);
            // 返回成功结果
            return CommonResult.ok().put(CommonResult.RETURN_RESULT, scheduleData);
        } catch (Exception e) {
            // 记录异常日志
            log.error("查询医生出诊排班时出错，form: {}", form, e);
            // 返回统一的失败信息
            return CommonResult.failed("查询医生出诊排班时出错");
        }
    }

    @Operation(summary = "根据workPlanId查询出诊计划")
    @PostMapping("/selectScheduleByWorkPlanId")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT", "DOCTOR:SELECT"}, mode = SaMode.OR)
    public CommonResult selectScheduleByWorkPlanId(@RequestBody @Valid SelectScheduleByWorkPlanIdForm form) {
        HashMap map = doctorWorkPlanScheduleService.selectScheduleByWorkPlanId(form.getWorkPlanId());
        return CommonResult.ok().put("result", map);
    }

    @Operation(summary = "更新医生出诊计划")
    @PostMapping("/updateSchedule")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT", "DOCTOR:UPDATE"}, mode = SaMode.OR)
    public CommonResult update(@RequestBody @Valid UpdateDoctorScheduleForm form) {
        Map param = BeanUtil.beanToMap(form);
        doctorWorkPlanScheduleService.update(param);
        return CommonResult.ok();
    }

    @Operation(summary = "删除医生出诊计划")
    @PostMapping("/deleteWorkPlan")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT", "DOCTOR:DELETE"}, mode = SaMode.OR)
    public CommonResult deleteWorkPlan(@RequestBody @Valid DeleteWorkPlanForm form) {
        doctorWorkPlanScheduleService.deleteWorkPlan(form.getWorkPlanId());
        return CommonResult.ok();
    }
}
